package mapgen.func {
import flash.display.MovieClip;

/**
 * @author kangkai
 * 根据一个swf来生成地图数据
 * 注意,flash的rotation是-180~180的
 */
public class MapDataWriter {
	
	private var mc_map:MovieClip;
	
	public function MapDataWriter() {
		
	}
	
	public function writeData(mc_map:MovieClip) : String {
		this.mc_map=mc_map;
		sOut="";
		//地图信息
		outputDD(mc_map.tf_id.text);
		outputDD(mc_map.tf_name.text);
		outputDD(mc_map.tf_detail.text);
		outputDD(mc_map.tf_max.text);
		outputDD(mc_map.tf_mode.text);
		outputDD(mc_map.tf_sec.text);
		outputDD(mc_map.mc_bg.width);//大小
				
		outputDD(mc_map.mc_bg.height);
		outputDD("#");

		//障碍物信息
		
		findBars();
		outputDD("#");
		//个玩家初始位置
		
		findTeamA();
		findTeamB();
		findTeamFree();
		outputDD("#");
		findProp();
		
		return sOut;
	}
	
	private var sOut:String="";
	private function outputDD(s : String) : void {
		sOut+=s+"\n";
	}

	//=========================函数===============
	//找出障碍物
	private function findBars():void {

		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.mc_wh == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;

			var CellA : int = mcc.mc_wh.width;
			var CellB : int = mcc.mc_wh.height;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}

			var outA : int = CellA;
			var outB : int = CellB;
			if(rot == 90 || rot == 270) {
				outA = CellB;
				outB = CellA;
			}

			//高度,注意是x2的，因为是以底面中心做碰撞的
			var outH : int = parseInt(mcc.tf_height.text) * 2;
			
			var type : int = parseInt(mcc.tf_type.text);
			var hp : int = parseInt(mcc.tf_hp.text);

			//根据类型，排放位置
			
			var outY : int = 0;//底面中心的y
			//如果是坑，则放低,就是把东西放到地平线以下，
			//这样，坑就只被坦克碰撞而不被炮弹碰撞了
			if(type == 0) {
				outY = -outH / 2;
			}


			outputDD(type + "," + hp + "," + outX + "," + outY + "," + outZ + "," + outA + "," + outB + "," + outH + "," + rot);
		}
	}

	//找出A队队员
	private function findTeamA():void {

		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_teamA == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}



			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出B队队员
	private function findTeamB():void {
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_teamB == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}



			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出自由人位置
	private function findTeamFree():void {
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_free == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}

			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出生道具的位置
	private function findProp():void{
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_prop == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;

			outputDD(outX + "," + outZ);
		}
	}
}//end of class
}
